import axios from 'axios';
const state = {
    detail: {},
    instructor: {},
    onFetching: false,
    unregistered: false,
    isShowNav: true
};

export default {
    state,
    getters: {
        getDetail: state => {
            if (JSON.stringify(state.detail) === '{}') {
                state.detail = JSON.parse(window.sessionStorage.getItem('courseDetail'));
            }
            return state.detail;
        },
        getInstructor: state => { //讲师信息
            return state.instructor;
        },
        getIsShowNav: state => {
            return state.isShowNav;
        },
        /**
         * 时间计算
         * @param state
         * @returns {function(*=, *=): string}
         */
        ultZeroize: (state) => (v, l) => {
            let z = '';
            l = l || 2;
            v = String(v);
            for (let i = 0; i < l - v.length; i++) {
                z += '0';
            }
            return z + v;
        },
        timeToStr: (store, getters) => (ts) => {
            if (isNaN(ts)) {
                return '--:--:--';
            }
            let h = parseInt(ts / 3600);
            let m = parseInt((ts % 3600) / 60);
            let s = parseInt(ts % 60);
            return (getters.ultZeroize(h) + ':' + getters.ultZeroize(m) + ':' + getters.ultZeroize(s));
        },
        /**
         * 学员是否已经成功报名
         * （等待队列与等待审批也算报名成功）
         * @param appStatus 报名学习状态
         * @returns {boolean}
         */
        hasApped: (state) => (appStatus) => {
            return !(!appStatus || appStatus === 'Rejected' || appStatus === 'Withdrawn' || appStatus === 'notapp');
        },
        /**
         * 学员是否已经成功报名2
         * （等待队列与等待审批也不算报名成功）
         * @param appStatus 报名学习状态
         * @returns {boolean}
         */
        hasAppPassed: (state, getters) => (appStatus) => {
            return !(!getters.hasApped(appStatus) || appStatus === 'Waiting' || appStatus === 'Pending');
        },
        getAppStatus: (state) => {
            return state.detail.app;
        },
        /**
         * 学员报名通过
         * @param state
         * @param getters
         * @returns {function(*=)}
         */
        enrollPassed: () => (appStatus) => {
            switch (appStatus) {
            case 'I'://正在进行中
            case 'C'://已完成
                return true;
            default:
                return false;
            }
        }
    },
    mutations: {
        increment(state, payload) {
            state.detail = payload;
            //防止刷新被重置
            window.sessionStorage.setItem('courseDetail', JSON.stringify(state.detail));
        },
        setScheduleList(state, data) {
            state.scheduleList = data;
            //防止刷新被重置
            window.sessionStorage.setItem('scheduleList', JSON.stringify(state.scheduleList));
        },
        setUnregistered(state, payload) {
            state.unregistered = payload;
        },
        setIsShowNav(state) { //设置页面碳层时时候否显示当前页面固定的头部和底部
            state.isShowNav = !state.isShowNav;
        },
        setInstructor(state, payload) { //设置讲师信息
            state.instructor = payload;
        }
    },
    actions: {
        /**
         * 课程返回时间保存
         * @param store
         * @param params
         * @returns {Promise<any>}
         */
        saveTime(store, params) {
            return new Promise((resolve, reject) => {
                let curDatetime = new Date();
                let orgTime = new Date(params.cur_time);
                let d = (curDatetime - orgTime) / 1000;
                let duration = store.getters.timeToStr(d);
                let status = params.data.status === 'null' ? 'I' : params.data.status;
                let call = resolve;
                params = Object.assign({
                    cmd: 'putparam',
                    lessonLocation: 'default',
                    time: duration,
                    lessonStatus: status,
                    encryptedStartTime: params.data.encryptedTime,
                    startTime: params.data.startTime,
                    entId: params.data.usrEntId
                    // courseEvaluationKey: {
                    //     covCosId: params.cosId,
                    //     covEntId: params.data.usrEntId,
                    //     covTkhId: params.tkhId
                    // },
                }, params);
                delete params.data;
                delete params.cmd;
                delete params.curTime;
                axios({
                    method: 'POST',
                    url: '/app/module/put-params',
                    data: params
                }).then(res => {
                    call(res);
                });
            });
        },
        /**
         * 报名在学一次
         * @param state
         * @param id 课程id 判断当前时候有课程id 没有就使用手动传入的id
         * @returns {Promise<any>}
         */
        contentTipBtnAction({state}, id) {
            let itmId = state.detail.item.itm_id ? state.detail.item.itm_id : id;
            return new Promise((resolve, reject) => {
                axios({
                    method: 'GET',
                    url: `/app/application/app?itm_id=${itmId}`
                }).then(res => {
                    resolve();
                });
            });
        },
        /**
         * 获取当前课程的信息
         * @param state
         * @param id
         * @returns {Promise<any>}
         */
        getCourseInfo({commit}, itemId, tkhId) {
            return new Promise(resolve => {
                axios({
                    method: 'POST',
                    url: '/app/course/detailJson/' + itemId,
                    params: {
                        tkhId: tkhId,
                        mobileInd: 1
                    }
                }).then(res => {
                    commit('increment', res.data);
                });
            });
        }
    }
};
